#include <stdio.h>
#include <string>
#include "gamemath.h"

	Uint32 c_red;
	Uint32 c_green;
	Uint32 c_blue;
float gamemath::anglefromline(int cx, int cy, int tx, int ty)
{
	float angle;
	float dX = tx - cx;
	float dY = ty - cy;
	angle = atan2(dY,dX);
	angle=angle*(180/3.14);
	//angle+=180;
	angle = (angle > 0.0 ? angle : (360.0 + angle));
	return angle;
}

float gamemath::extendlinex(int cx, int cy, int tx, int ty, int l)
{
	float angle;
	float dX = tx - cx;
	float dY = ty - cy;
	angle = atan2(dY,dX);// calculate the angle by giving atan2 the length of the two adjecent lines (soh cah toa)
	return l*cos(angle) +cx;
}

float gamemath::extendliney(int cx, int cy, int tx, int ty, int l)
{
	float angle;
	float dX = tx - cx;
	float dY = ty - cy;
	angle = atan2(dY,dX);// calculate the angle by giving atan2 the length of the two adjecent lines (soh cah toa)

	return l*sin(angle) +cy;
}

float gamemath::angleoffsetx(int x, float angle, int offset)
{
	angle=(angle/180)*3.14;
	return offset*cos(angle) + x;
}

float gamemath::angleoffsety(int y, float angle, int offset)
{
	angle=(angle/180)*3.14;
	return offset*sin(angle) + y;
}

Uint32 gamemath::getpixel(SDL_Surface *surface, int x, int y)
{
    int bpp = surface->format->BytesPerPixel;
    /* Here p is the address to the pixel we want to retrieve */
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

    switch (bpp) {
    case 1:
        return *p;

    case 2:
        return *(Uint16 *)p;

    case 3:
        if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
            return p[0] << 16 | p[1] << 8 | p[2];
        else
            return p[0] | p[1] << 8 | p[2] << 16;

    case 4:
        return *(Uint32 *)p;

    default:
        return 0;       /* shouldn't happen, but avoids warnings */
    } // switch
}

polarcoord gamemath::getSpriteGuideOffset(SDL_Surface* sprite, int r, int g, int b)
{

	SDL_LockSurface(sprite);
	Uint32 col = SDL_MapRGB(sprite->format, r, g, b);
	int foundx,foundy;
	int width = sprite->w;
	int height = sprite->h;
	polarcoord output;
	for(int x = 0; x<=width; x++)
	{
		for(int y = 0; y<=height; y++)
		{
			if(getpixel(sprite,x,y) == col)
			{
				cout <<endl;
				cout << "COLOR FOUND AT: " <<endl;
				cout << "X: " << x <<endl;
				cout << "Y: " << y <<endl;
				foundx = x;
				foundy = y;
			}
		}
	}

	//normalize foundx/y and calculate polar theta:
	foundx -= (width/2);
	foundy -= (height/2);

	output.radius = sqrt((float)(foundx*foundx) + (foundy*foundy));
	output.theta = atan2((float)foundy,(float)foundx);
	cout << "polar coord: " <<endl;
	cout << "radius: " << output.radius <<endl;
	cout << "theta: " << output.theta*(180/3.14) <<endl;
	SDL_UnlockSurface(sprite);
	return output;
}

float gamemath::relativepolarx(float r_angle,float n_angle, float length)
{
	return length*cos(r_angle*(3.14159265 / 180)+n_angle);
}

float gamemath::relativepolary(float r_angle,float n_angle, float length)
{
	return length*sin(r_angle*(3.14159265 / 180)+n_angle);
}

float gamemath::polarx(float a, float r)
{
	return r*cos(a);
}

float gamemath::polary(float a, float r)
{
	return r*sin(a);
}

void gamemath::parsemap(const char* ImageFileName, char temp[][255],int& rowsize, int& colsize)
{
	bool foundrowsize = false;
	char c;
	string rowbuffer[50];
	int cursor = 0;
	FILE * input;
	input = fopen( "rsc/map1.map" ,"r");
	int id,xx,yy;
	string idbuf,xbuf,ybuf;
	//find how many characters are in the first row
	do{
		c = getc(input);
			cursor++;
		if(c == '\n')
		{
			colsize++;
		}
		else
		{
			rowbuffer[colsize] = rowbuffer[colsize] + c;
		}
	} while (c!=';');

	do{
	c = getc(input);
	if(c=='{')
	{
		//{0,64,64}
		idbuf="";
		xbuf="";
		ybuf="";
		c = getc(input);

		while(c!=',')
		{
		idbuf += c;
		c = getc(input);
		}

		c = getc(input);

		while(c!=',')
		{
		xbuf += c;
		c = getc(input);
		}

		c = getc(input);

		while(c!='}')
		{
		
		ybuf += c;
		c = getc(input);
		}

		g_items->addItem(atoi(xbuf.c_str()),atoi(ybuf.c_str()),atoi(idbuf.c_str()));
	}
	}while(c!=EOF);
	fclose(input);
	colsize++;
	rowsize = rowbuffer[0].length();
	for(int y = 0; y<colsize; y++)
	{
		for(int x = 0; x<rowsize; x++)
		{
			temp[x][y] = rowbuffer[y].at(x);
		}
	}
}
